package com.app.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.R;
import com.app.constant.Constants;
import com.app.constant.Defaultcontent;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.request.ReqObj;
import com.app.model.response.InviteRes;
import com.app.model.response.ResObj;
import com.app.utils.DensityUtil;
import com.app.utils.QrcodeUtil;
import com.app.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ActivityContactUs extends ActivityBaseMenuText {


    @BindView(R.id.ivQrcode)
    ImageView ivQrcode;
    @BindView(R.id.tvTip)
    TextView tvTip;

    private HttpDataLoader dataLoader;

    private String inviteUrl = Defaultcontent.inviteUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_us);
        ButterKnife.bind(this);
        setTitle("联系我们");
        dataLoader = new HttpDataLoader(this);
        getInvitecode();
    }


    private void getInvitecode() {
        ReqObj req = new ReqObj();
        dataLoader.POST(HttpDataReqUrl.URL_GET_INVITE_CODE, req);
    }

    private void initView() {
        Bitmap qrBitmap = QrcodeUtil.generateBitmap(inviteUrl, DensityUtil.dip2px(200), DensityUtil.dip2px(200));
        Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Bitmap bitmap = QrcodeUtil.addLogo(qrBitmap, logoBitmap);
        ivQrcode.setImageBitmap(bitmap);
    }


    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        if (url.equals(HttpDataReqUrl.URL_GET_INVITE_CODE)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                if (result.getData() instanceof InviteRes) {
                    InviteRes res = (InviteRes) (result.getData());
                    inviteUrl = res.getInviteUrl();
                }
            } else {
                ToastUtil.getInstance().showToast(this, result.getMsg());
            }
            initView();
        }
    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        ToastUtil.getInstance().showToast(this, error);
        initView();
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.push_left_out);
    }
}
